import 'dart:convert';

import 'package:oktoast/oktoast.dart';

import '../models/follow/follow_model.dart';
import '../providers/user_context.dart';
import '../utils/HttpUtil.dart';

class FollowDao {
  static const String FOLLOW_FRIEND = "/api/user/relation/v1/friend/query";
  static const String FOLLOW_BLOCK = "/api/user/relation/v1/friend/blocked";

  static const String FOLLOW_USER = "/api/user/relation/v1/follow/change";
  static const String BLOCK_USER = "/api/user/relation/v1/block/change";

  static Future<List<FollowModel>> fetchAllContact(int pageIdx) async {
    var response = await HttpUtil.getInstance().get(
        "/api/user/relation/v1/all/query",
        data: {'uid': UserContext.cxt()!.uid, 'pageIdx': pageIdx});
    if (response?.statusCode == 200) {
      return response!.data['data']
          .map<FollowModel>((ele) => FollowModel.fromJson(ele))
          .toList();
    } else {
      throw Exception('Failed to load home_page.json');
    }
  }

  static Future<List<FollowModel>> fetchFriend() async {
    var response = await HttpUtil.getInstance()
        .get(FOLLOW_FRIEND, data: {'uid': UserContext.cxt()!.uid});
    if (response?.statusCode == 200) {
      return response!.data['data']
          .map<FollowModel>((ele) => FollowModel.fromJson(ele))
          .toList();
    } else {
      throw Exception('Failed to load home_page.json');
    }
  }

  static Future<List<FollowModel>> fetchBlock() async {
    var response = await HttpUtil.getInstance()
        .get(FOLLOW_FRIEND, data: {'uid': UserContext.cxt()!.uid});
    if (response?.statusCode == 200) {
      return response!.data['data']
          .map<FollowModel>((ele) => FollowModel.fromJson(ele))
          .toList();
    } else {
      throw Exception('Failed to load home_page.json');
    }
  }

  static Future<void> followUser(num targetUid, bool isFollow) async {
    var response =
        await HttpUtil.getInstance().post("/api/user/relation/v1/follow/change",
            data: jsonEncode({
              "fromUserId": UserContext.cxt()!.uid,
              "targetUserId": targetUid,
              "isFollow": isFollow
            }));
    if (response?.statusCode == 200) {
      if (isFollow)
        showToast("已经关注");
      else
        showToast("成功");
    }
  }
}
